package com.tedu.user.service.impl;

import com.tedu.account.mapper.AccountMapper;
import com.tedu.account.pojo.dto.AccountSaveParam;
import com.tedu.account.pojo.vo.AccountVO;
import com.tedu.account.service.AccountService;
import com.tedu.emp.pojo.dto.EmpQuery;
import com.tedu.emp.pojo.vo.EmpVO;
import com.tedu.emp.service.EmpService;
import com.tedu.guardian.mapper.GuardianMapper;
import com.tedu.guardian.pojo.vo.GuardianVO;
import com.tedu.guardian.service.GuardianService;
import com.tedu.user.mapper.UserMapper;
import com.tedu.user.pojo.dto.UserParam;
import com.tedu.user.pojo.dto.UserQuery;
import com.tedu.user.pojo.entity.User;
import com.tedu.user.pojo.vo.UserVO;
import com.tedu.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    GuardianService guardianService;
    @Autowired
    GuardianMapper guardianMapper;
    @Autowired
    EmpService empService;
    @Autowired
    AccountService accountService;
    @Autowired
    AccountMapper accountMapper;

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("查询用户信息，参数:{}",userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        //查询老人对应的监护人
        log.debug("查询用户对应的监护人");
        for (UserVO userVO : list) {
            Integer guardianId = userVO.getGuardianId();
            if(guardianId!=null){
                GuardianVO guardianVO = guardianService.selectGuardianById(guardianId);
                //设置监护人姓名和手机号
                userVO.setGuardianName(guardianVO.getName());
                userVO.setGuardianPhone(guardianVO.getPhone());
            }
        }
        //从员工列表中查询用户对应的员工
        log.debug("查询用户对应的员工");
        for (UserVO userVO : list) {
            EmpQuery empQuery = new EmpQuery();
            empQuery.setId(userVO.getEmpId());
            List<EmpVO> empVOS = empService.selectEmpl(empQuery);
            if(empVOS.size()>0){
                userVO.setEmpName(empVOS.get(0).getEmpName());
            }
        }

        return list;
        }

    @Override
    @Transactional
    //保存用户和监护人信息
    public void saveUser(UserParam userParam) {
        log.debug("保存用户业务,参数:{}", userParam);
        User user = new User();
        BeanUtils.copyProperties(userParam, user);
        AccountSaveParam accountSaveParam = new AccountSaveParam();
        //添加用户
        if (user.getId() == null) {
            user.setCreateTime(new Date());
            user.setAge(user.getCreateTime().getYear() - user.getBirth().getYear());
            guardianService.insertGuardian(userParam);
            //将插入的监护人id传给用户的guardianId属性
            GuardianVO guardianVO = guardianMapper.selectGuardian(userParam.getGuardianName());
            user.setGuardianId(guardianVO.getId());
            //调用查询员工接口，将员工的id传给用户的empId属性
            EmpQuery empQuery = new EmpQuery();
            empQuery.setEmpName(userParam.getEmpName());
            List<EmpVO> empVOS = empService.selectEmpl(empQuery);
            user.setEmpId(empVOS.get(0).getId());
            //设置用户创建时间
            user.setCreateTime(new Date());
            //计算用户的年龄
            user.setAge(user.getCreateTime().getYear() - user.getBirth().getYear());
            userMapper.insert(user);
            log.debug("添加用户账户,参数:{}",user);
            accountSaveParam.setUsername(user.getUsername());
            accountSaveParam.setCard("JSDN"+userParam.getNum().substring(user.getNum().length()-6));
            accountSaveParam.setUserId(user.getId());
            accountSaveParam.setOrganization(userParam.getOrganization());
            accountService.saveAccount(accountSaveParam);
        }else{
            //更新监护人信息
            UserVO userVO=userMapper.selectUserById(user.getId());
            GuardianVO guardianVO1=guardianMapper.selectGuardianById(userVO.getGuardianId());
            //判断该用户是否存在监护人信息，不存在则插入并删除原来的监护人对象
            if(guardianVO1==null){
                guardianService.insertGuardian(userParam);
                guardianService.deleteGuardianById(userVO.getGuardianId());
            }else{
                //如果有监护人信息 则更新监护人信息
                guardianVO1.setName(userParam.getGuardianName());
                guardianVO1.setPhone(userParam.getGuardianPhone());
                guardianService.updateGuardian(userParam);
            }
            //更新用户
            user.setUpdateTime(new Date());
            user.setAge(user.getUpdateTime().getYear() - user.getBirth().getYear());
            //更新用户
            userMapper.update(user);
            log.debug("更新用户账户,参数:{}",user);
            AccountVO accountVO =accountMapper.selectAccountByUserId(user.getId());
            BeanUtils.copyProperties(userParam, accountSaveParam);
            accountSaveParam.setId(accountVO.getId());
            accountSaveParam.setCard(user.getNum().substring(user.getNum().length()-6));
            log.debug("修改账户：{}",accountSaveParam);
            accountService.saveAccount(accountSaveParam);
        }
    }

    @Override
    public void deleteUser(Integer userId) {
        log.debug("删除用户业务,参数:{}",userId);
        UserVO userVO = userMapper.selectUserById(userId);
        userMapper.deleteUser(userId);
        accountMapper.deleteAccount(userVO.getId());
        // 如果用户有监护人，删除监护人信息
        if(userVO.getGuardianId()!=null){
            guardianService.deleteGuardianById(userVO.getGuardianId());
        }
    }
}

